package org.yjc.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yjc.custom.DateUtils;
import org.yjc.custom.JqgridParam;
import org.yjc.custom.JqgridUtils;
import org.yjc.entity.Eim;
import org.yjc.entity.KingRoot;
import org.yjc.entity.Lessee;
import org.yjc.entity.Log;
import org.yjc.entity.SysUser;
import org.yjc.form.KingRootForm;
import org.yjc.form.LesseeForm;
import org.yjc.security.UserUtils;
import org.yjc.service.KingRootService;
import org.yjc.service.LesseeAdminService;
import org.yjc.service.LesseeService;
import org.yjc.service.LogService;
import org.yjc.service.SysUserService;

import ch.qos.logback.core.joran.util.beans.BeanUtil;

@Controller
@RequestMapping(value="/lessee")
public class LesseeController {
	@Autowired
	private LesseeService lesseeservice;
	@Autowired
	private LesseeAdminService lesseeadminservice;
	@Autowired
	private KingRootService kingrootservice;
	@Autowired
	private UserUtils userutils;
	@Autowired 
	private LogService logservice;
	
	@RequestMapping(value="/trtenantmain")
	public String trtenant(ModelMap map) {
		SysUser sysuser = userutils.getUser();
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String username = userDetails.getUsername();
		sysuser = lesseeadminservice.findByUsername(username);
		Integer id = sysuser.getId();
		map.put("lesseeadminId", id);
		return "lessee/trtenantmain";
	}
	
	@RequestMapping(value="/list")
	@ResponseBody
	public Object list(JqgridParam param,Integer lesseeadminId) {
		Pageable pageable = param.buildPageable();
		Specification spec = new Specification<KingRoot>() {

			@Override
			public Predicate toPredicate(Root<KingRoot> root, CriteriaQuery<?> arg1, CriteriaBuilder cb) {
				Predicate predicate = cb.equal(root.get("lessee").get("lesseeadmin").get("id"), lesseeadminId);
				return predicate;
			}
		};
		Page<KingRoot> page;
		if(param.getSearch()) {
			spec = spec.and(param.buildSpec());
		}
		page = kingrootservice.findAll(spec,pageable);
		return JqgridUtils.getPageResult(page);
	}
	
	@RequestMapping(value="/edit")
	@ResponseBody
	public Object edit(KingRootForm form) {
		String oper = form.getOper();
		KingRoot kingroot = new KingRoot();
		if("del".equals(oper)) {
			kingrootservice.deleteById(form.getId());
		}else {
			if("edit".equals(oper)) {
				kingroot = kingrootservice.findById(form.getId());
			}
			BeanUtils.copyProperties(form, kingroot,"id");
			kingroot.setStatime(DateUtils.currentTime()); //存入当前系统时间
			kingroot.setEndtime(DateUtils.laterTime()); //存入一年后的时间
			kingroot.setMoney(0);
			SysUser user = userutils.getUser();	 //获取当前用户
			Log log = new Log();
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
			log.setName(user.getUsername());
			log.setOperationTime(df.format(new Date())); //获取当前系统时间
			log.setOperatiobName("新增租户");
			logservice.save(log);
			kingrootservice.save(kingroot);
		}
		return "OK";
	}
	
	
}
